package com.ktjiaoyu.server.controller;

import com.ktjiaoyu.server.pojo.Menu;
import com.ktjiaoyu.server.service.IMenuService;
import com.ktjiaoyu.server.vo.RespBean;
import com.ktjiaoyu.server.vo.RoleMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/system/cfg")
@Api("菜单模块")
public class MenuController {
    @Resource
    IMenuService menuService;

    @ApiOperation(value = "通过用户ID查询菜单列表")
    @GetMapping("/menu")
    public List<Menu> getMenusByAdminId() {
        return menuService.getMenusByAdminId();
    }

    @ApiOperation("获取所有的菜单列表(带层级关系用于绑定树形菜单控件)")
    @GetMapping("/getAllMenuList")
    public RespBean getAllMenuList() {
        List<Menu> list = menuService.getAllMenuList();
        return RespBean.success("查询所有菜单成功",list);
    }

    @ApiOperation("根据角色ID查询当前角色所拥有的权限id集合(格式为以逗号分隔的字符串)")
    @GetMapping("/getMenuIdsByRoleId/{roleId}")
    public RespBean getMenuIdsByRoleId(@PathVariable("roleId") Integer roleId) {
        RoleMenuVo vo= menuService.getMenuRoleList(roleId);
        return RespBean.success("获取角色已分配权限成功",vo);
    }

    @ApiOperation("保存菜单信息")
    @PostMapping("/saveMenu")
    public RespBean saveMenu(@RequestBody Menu menu) {
        if(menuService.saveMenu(menu)) {
            return RespBean.success("菜单信息保存成功!");
        } else {
            return RespBean.error("菜单信息保存失败!");
        }
    }

    @ApiOperation("通过菜单id查询菜单详情对象信息")
    @GetMapping("/findByMenuId/{id}")
    public RespBean getById(@PathVariable("id")Integer id) {
        Menu menu=menuService.getById(id);
        return RespBean.success("编辑菜单成功",menu);
    }

    @ApiOperation("保存修改后菜单信息")
    @PostMapping("/updateMenu")
    public RespBean updateMenu (@RequestBody Menu menu) {
        if (menuService.updateMenu (menu)) {
            return RespBean.success("修改菜单信息成功！");
        } else {
            return RespBean.error("修改菜单信息失败");
        }
    }

    @ApiOperation("根据菜单ID删除指定的菜单信息")
    @RequestMapping("/delMenuById/{id}")
    public RespBean delMenuById(@PathVariable("id")Integer id) {
        if(menuService.delMenuById(id)) {
            return RespBean.success("菜单信息删除成功!");
        } else {
            return RespBean.error("菜单信息删除失败!");
        }
    }



}
